﻿using App.Core.Services;
using AppTemplate.UI.Framework;
using AppTemplate.UI.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace AppTemplate.UI.ViewModels
{
    public partial class LoginViewModel : ViewModel
    {
        private readonly IUserService userService;

        public LoginViewModel(IUserService userService)
        {
            this.userService = userService;
        }

        [ObservableProperty]
        private string? account="admin";


        [RelayCommand]
        private async Task Login(object passwordBox)
        {
            

            if (string.IsNullOrEmpty(Account))
            {
                App.Alert("用户名不能为空");
                return;
            }
            var password = (passwordBox as PasswordBox)!.Password;
            if (password == null)
            {
                App.Alert("密码不能为空");
                return;
            }
            this.IsLogining = true;
            var user = await userService.Auth(Account, password);
            this.IsLogining = false;

            if (user == null)
            {
                App.Alert("用户名或密码不正确");
            }
            else
            {
                App.Go<TabView>();
            }
           
        }

        [RelayCommand]
        private void Reset(object passwordBox)
        {
            Account = null;
            ((PasswordBox)passwordBox).Password = null;

            App.Width = 200;
            App.Height = 200;
        }

        [ObservableProperty]
        private bool isLogining;
    }
}
